package com.nahan.secondapplication.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.nahan.secondapplication.R;

/**
 * activity 父类  可扩展
 * @param <T>
 */
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {

    protected T mPresenter;

    private View rootView;
    private CollapsingToolbarLayout contentLayout;
    private LayoutInflater layoutInflater;
    private LoadingLayout loadingLayout;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        contentLayout = (CollapsingToolbarLayout) findViewById(R.id.content_layout);
        loadingLayout = (LoadingLayout) findViewById(R.id.loading_view);
        layoutInflater = LayoutInflater.from(this);
        rootView = layoutInflater.inflate(setContentView(), null);
        contentLayout.addView(rootView);
        init();

    }

    public abstract int setContentView();

    public abstract void init();

    /**
     * 开始加载
     */
    public void ShowLoading()
    {
        loadingLayout.showLoading();
    }

    /**
     * 加载成功
     * @param state
     */
    public void ShowLoadSuccess(int state)
    {
        loadingLayout.refreshView(state);
    }

    /**
     * 加载失败
     * @param onClickListener
     */
    public void ShowLoadFailureMessage(View.OnClickListener onClickListener)
    {
        loadingLayout.showLoadFailureMessage(onClickListener);
    }

    /**
     * 没有网络的时候
     * @param onClickListener
     */
    public void ShowNetErrorMessage(View.OnClickListener onClickListener)
    {
        loadingLayout.showNetErrorMessage(onClickListener);
    }
    /**
     * 没有数据
     */
    public void ShowNullDataMessage()
    {
        loadingLayout.showNullDataMessage();
    }


    /**
     * 销毁view 避免p层持有造成内存泄露
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.detachView();

        }

    }



}
